<template>
    <div>
        <el-main>

            <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>公告</el-breadcrumb-item>
            </el-breadcrumb>

            <div style="padding: 10px 0">
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"> </el-input>
                <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
                <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
            </div>
            <div style="margin: 10px 0">
                <el-button type="primary" @click="newadd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
                <el-popconfirm
                        confirm-button-text='确定'
                        cancel-button-text='我在想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="这是一段内容确定删除吗？"
                        @confirm="alldelete"
                >
                    <el-button type="danger"  slot="reference" style="margin: 0px 15px">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>
            </div>

            <el-table :data="tableData" border stripe  @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column prop="id" label="ID" width="80">
                </el-table-column>
                <el-table-column prop="name" label="标题" width="120">
                </el-table-column>
                <el-table-column prop="content" label="内容">
                    <template v-slot="scope"><el-button type="primary" @click="view(scope.row.content)">查看内容</el-button></template>
                </el-table-column>
                <el-table-column prop="time" label="发布时间">
                </el-table-column>
                <el-table-column prop="user" label="发布人">
                </el-table-column>
                <el-table-column label="图片"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image></template></el-table-column>
                <el-table-column label="操作"  width="200" align="center">
                    <template slot-scope="scope">
                        <el-button type="success" @click="update(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                        <el-popconfirm
                                confirm-button-text='确定'
                                cancel-button-text='我在想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="这是一段内容确定删除吗？"
                                @confirm="remove(scope.row.id)"
                        >
                            <el-button type="danger" slot="reference"  style="margin-left: 15px">删除 <i class="el-icon-remove-outline"></i></el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 10px 0">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageNum"
                        :page-sizes="[2, 5, 10]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>


                <el-dialog :title="title" :visible.sync="dialogFormVisible"  width="60%">
                    <el-form :model="form" label-width="100px" size="small" style="width: 90%">
                        <el-form-item label="标题" >
                            <el-input v-model="form.name" autocomplete="off"></el-input>
                        </el-form-item>

                        <el-form-item label="封面" >
                            <el-upload
                                    class="avatar-uploader"
                                    action="http://localhost:9090/file/upload"
                                    ref="img"
                                    :show-file-list="false"
                                    :on-success="handleAvatarSuccess"
                                    >
                                <img v-if="form.img" :src="form.img" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>

                        <el-form-item label="内容" >
<!--                            <el-input type="textarea" v-model="form.content" autocomplete="off"></el-input>-->
                       <div id="rich" style=""></div>
                        </el-form-item>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>

                <el-dialog title="内容" :visible.sync="dialogFormVisible1"  width="60%">
                    <div v-html="content"></div>
                </el-dialog>

            </div>
        </el-main>
    </div>
</template>

<script>
    import request from "@/utils/request";
    import E from "wangeditor";
    let editor;
    export default {
        name: "User",
        data(){
            return {
                tableData: [],
                total:0,
                pageSize:5,
                pageNum:1,
                name:"",
                dialogFormVisible:false,
                dialogFormVisible1:false,
                content:'',
                title:'新增',
                form:{},
                multipleSelection: [],
                Inuser:JSON.parse(localStorage.getItem("Inuser"))
            }
        },
        created() {
            this.load()
        },
        methods:{
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            load(){
                request.get("http://localhost:9090/notice/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&name="+this.name).then(res=>{
                    console.log()
                    this.tableData=res.records
                    this.total=res.total
                })

            },
            reset(){
                this.name=""
                this.load()
            },
            newadd(){
                this.title="新增"
                this.dialogFormVisible=true
                this.form={img:''}
                this.$nextTick(()=>{
                    editor  = new E("#rich")
                    editor.config.uploadImgServer='http://localhost:9090/file/uploadImg'
                    editor.config.uploadFileName ='file'
                    editor.create()
                })
                editor.txt.html("")
            },
            view(e){
                this.content=e
                this.dialogFormVisible1=true
            },
            Add(){
                this.form.content=editor.txt.html()
                request.post("http://localhost:9090/notice",{notice:this.form,name:this.Inuser.username},'json' ).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
                //this.form=""
                this.dialogFormVisible=false
            },
            remove(e){
                request.delete("http://localhost:9090/notice/"+e).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            alldelete(){
                let ids=this.multipleSelection.map(v=>v.id)
                request.delete("http://localhost:9090/notice/all",{data:ids}).then(res=>{
                    if(res){
                        this.$message.success("批量删除成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            update(e){
                this.title='修改'
                this.form=e
                this.dialogFormVisible=true
                this.$nextTick(()=>{
                    if (!editor){
                        editor  = new E("#rich")
                        editor.config.uploadImgServer='http://localhost:9090/file/uploadImg'
                        editor.config.uploadFileName ='file'
                        editor.create()
                    }editor.txt.html(this.form.content)
                })
            },
            handleAvatarSuccess(res){
                console.log(res)
                this.form.img=res
            }
        }
    }
</script>

<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }

</style>